package com.ruoyi.project.cwsk.cpgame.controller;

import java.io.IOException;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.cwsk.cpgame.domain.CpGame;
import com.ruoyi.project.cwsk.cpgame.service.ICpGameService;

/**
 * 推广游戏Controller
 * 
 * @author songzc
 * @date 2020-01-10
 */
@Controller
@RequestMapping("/cwsk/cpgame")
public class CpGameController extends BaseController{
	
	
	private static final Logger log = LoggerFactory.getLogger(CpGameController.class);
	
    private String prefix = "cwsk/cpgame";

    @Autowired
    private ICpGameService cpGameService;

    @RequiresPermissions("cwsk:cpgame:view")
    @GetMapping()
    public String cpgame()
    {
        return prefix + "/cpgame";
    }

    /**
     * 查询推广游戏列表
     */
    @RequiresPermissions("cwsk:cpgame:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CpGame cpGame)
    {
        start2Page();
        List<CpGame> list = cpGameService.selectCpGameList(cpGame);
        return getDataTable(list);
    }

    /**
     * 导出推广游戏列表
     */
    @RequiresPermissions("cwsk:cpgame:export")
    @Log(title = "推广游戏", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CpGame cpGame)
    {
        List<CpGame> list = cpGameService.selectCpGameList(cpGame);
        ExcelUtil<CpGame> util = new ExcelUtil<CpGame>(CpGame.class);
        return util.exportExcel(list, "cpgame");
    }

    /**
     * 新增推广游戏
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存推广游戏
     */
    @RequiresPermissions("cwsk:cpgame:add")
    @Log(title = "推广游戏", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestParam(name="gameIconFullFile",required=false) MultipartFile gameIconFullFile,
    		@RequestParam(name="gameIconFile",required=false) MultipartFile gameIconFile,CpGame cpGame){
    	
    	if (!gameIconFullFile.isEmpty()){
    		try {
    			String gameIconFull = FileUploadUtils.upload(RuoYiConfig.getGamePath(), gameIconFullFile);
    			cpGame.setGameIconFull(gameIconFull);
    		} catch (IOException e) {
    			log.error("大logo上传失败！", e);
                return error(e.getMessage());
    		}
    	}
    	if (!gameIconFile.isEmpty()){
    		try {
    			String gameIcon = FileUploadUtils.upload(RuoYiConfig.getGamePath(),  gameIconFile);
    			cpGame.setGameIcon(gameIcon);
    		} catch (IOException e) {
    			log.error("小logo上传失败！", e);
                return error(e.getMessage());
    		}
    	}
    	
    	
        return toAjax(cpGameService.insertCpGame(cpGame));  
    }

    /**
     * 修改推广游戏
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        CpGame cpGame = cpGameService.selectCpGameById(id);
        mmap.put("cpGame", cpGame);
        return prefix + "/edit";
    }

    /**
     * 修改保存推广游戏
     */
    @RequiresPermissions("cwsk:cpgame:edit")
    @Log(title = "推广游戏", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CpGame cpGame)
    {
        return toAjax(cpGameService.updateCpGame(cpGame));
    }

    /**
     * 删除推广游戏
     */
    @RequiresPermissions("cwsk:cpgame:remove")
    @Log(title = "推广游戏", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(cpGameService.deleteCpGameByIds(ids));
    }
}
